package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.db.DaoUtils;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.EcCategoryThird;
import com.shaoshaohuo.app.entity.SupplyRecord;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.PublishManagerActivity;
import com.shaoshaohuo.app.ui.ec.BuysGoodsActivity;
import com.shaoshaohuo.app.ui.ec.SellsGoodsActivity;
import com.shaoshaohuo.app.utils.AnimationUtil;
import com.shaoshaohuo.app.utils.StringHelper;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.shaoshaohuo.app.view.MessageDialog;

import java.util.List;

public class SupplyRecordListAdapter<T> extends MyBaseAdapter<SupplyRecord> implements View.OnClickListener {

	public SupplyRecordListAdapter(Context ctx, List<SupplyRecord> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {

		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.view_supply_record_item, null);
			holder = new ViewHolder();
			holder.numberText = (TextView) convertView.findViewById(R.id.textview_gongyingbianhao);
			holder.productNameText = (TextView) convertView.findViewById(R.id.textview_gongyingchanpin);
			holder.productCatNameText = (TextView) convertView.findViewById(R.id.textview_catname);
//			holder.standardText = (TextView) convertView.findViewById(R.id.textview_guige);
			holder.priceText = (TextView) convertView.findViewById(R.id.textview_price);
			holder.listedTimeText = (TextView) convertView.findViewById(R.id.textview_listed_time);
			holder.mCdateText = (TextView) convertView.findViewById(R.id.textview_cdate);
			holder.mBrowseText = (TextView) convertView.findViewById(R.id.textview_browse_count);
			holder.editText = (TextView) convertView.findViewById(R.id.textview_edit);
			holder.freshLayout = (LinearLayout) convertView.findViewById(R.id.layout_fresh);
			holder.editLayout = (LinearLayout) convertView.findViewById(R.id.layout_edit);
			holder.deleteLayout = (LinearLayout) convertView.findViewById(R.id.layout_delete);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		SupplyRecord item = list.get(pos);

		holder.numberText.setText("供应编号："+item.getNumber());
		holder.productNameText.setText("供应产品："+item.getTitle());

		holder.productCatNameText.setText(item.getcName());
//		 holder.standardText.setText("产品规格："+item.getProductnot());
		holder.priceText.setText("产品单价：￥"+item.getMoney()+"/"+item.getUnit());
		if(TextUtils.isEmpty(item.getStart())){
			if(TextUtils.isEmpty(item.getEnd())){
				holder.listedTimeText.setText("上市时间：至长期");
			}else{
				holder.listedTimeText.setText("上市时间：至"+item.getEnd());
			}
		}else{
			if(TextUtils.isEmpty(item.getEnd())){
				holder.listedTimeText.setText("上市时间："+item.getStart()+"至长期");
			}else{
				holder.listedTimeText.setText("上市时间："+item.getStart()+"-"+item.getEnd());
			}
		}
		holder.listedTimeText.setVisibility(View.GONE);
		holder.mCdateText.setText("发布日期："+StringHelper.formatDateMinute(item.getCdate()));
		holder.mBrowseText.setText("浏览量："+item.getVnum()+"次");

		holder.editText.setVisibility(View.GONE);


		holder.freshLayout.setTag(R.id.textview_cdate, holder.mCdateText);
		holder.freshLayout.setTag(item.getNumber());
		holder.editLayout.setTag(item.getId());
		holder.deleteLayout.setTag(item.getNumber());

		holder.freshLayout.setOnClickListener(this);
		holder.editLayout.setOnClickListener(this);
		holder.deleteLayout.setOnClickListener(this);
		return convertView;
	}

	@Override
	public void onClick(final View v) {
		int viewId = v.getId();
		switch (viewId) {
			case R.id.layout_fresh: {
				String recordId = (String) v.getTag();
				View updateTV = (View) v.getTag(R.id.textview_cdate);

				requestDataFresh(recordId, updateTV);
			}
			break;
			case R.id.layout_edit: {
				String goodsId = (String) v.getTag();

				Intent intent = new Intent(context, SellsGoodsActivity.class);
				intent.putExtra(BuysGoodsActivity.EXTRA_RECORD_ID, goodsId);
				context.startActivity(intent);
			}
			break;
			case R.id.layout_delete: {
				MessageDialog dialog = new MessageDialog(context);
				dialog.setMessage("确认要删除吗？");
				dialog.setLeftBtText("取消");
				dialog.setRightBtText("确认");
				dialog.setOnBtClickListener(new MessageDialog.OnBtClickListener() {
					@Override
					public void onLeftBtClick() {
					}

					@Override
					public void onRightBtClick() {
						String goodsId = (String) v.getTag();
						requestDataDelete(goodsId);
					}
				});
				dialog.show();
			}
			break;
			default:
				break;
		}
	}

	private void requestDataFresh(String recordId, final View updateTV) {
		RequestService.getInstance().publishedSupplyRecordRefresh(context, recordId, BaseEntity.class, new HttpRequest.RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				if (resultData.isOk()) {
					((TextView) updateTV).setText("发布日期：" + StringHelper.formatDateMinute("" + System.currentTimeMillis()));
					AnimationUtil.emphasize(context, updateTV, 0);
					ToastUtil.toast("刷新置顶成功");
				} else {
					ToastUtil.toast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				ToastUtil.toast(msg);
			}
		});
	}



	private void requestDataDelete(String recordId) {
		RequestService.getInstance().publishedSupplyRecordDelete(context, recordId, BaseEntity.class, new HttpRequest.RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				if (resultData.isOk()) {
					ToastUtil.toast("删除成功");
					((PublishManagerActivity) context).onRefresh();
				} else {
					ToastUtil.toast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				ToastUtil.toast(msg);
			}
		});
	}

	static class ViewHolder {
		TextView numberText;
		TextView productNameText;
		TextView productCatNameText;
		//		TextView standardText;
		TextView priceText;
		TextView listedTimeText;
		TextView mCdateText;
		TextView mBrowseText;

		TextView editText;
		LinearLayout freshLayout;
		LinearLayout editLayout;
		LinearLayout deleteLayout;
	}

}
